package com.rabbit.videoplayer.widget.gesture

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import coil.Coil
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.load
import com.rabbit.videoplayer.listener.OnVideoStatusListener
import com.rabbit.videoplayer.R

/**
 * 手势滑动的view。用于UI中处理手势的滑动事件，从而去实现手势改变亮度，音量，seek等操作。
 */
class GestureView : ConstraintLayout, OnVideoStatusListener {
    //手势控制
    private var mGestureControl: GestureControl? = null
    //监听器
    private var mOutGestureListener: GestureListener? = null
    //    //隐藏原因
    //    private HideType mHideType = null;
    //是否锁定屏幕
    private var isScreenLocked = false
    //是否处于分屏模式
    private var isInMultiWindow = false
    // 第一帧开始显示，才允许触控条件
    private var isFirstFrameStart = false

    private lateinit var ivLongPress: ImageView
    private lateinit var layoutLongPressFast: View

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {
        val rootView = LayoutInflater.from(context).inflate(R.layout.view_wb_mix_gesture, this, true)
        ivLongPress = rootView.findViewById(R.id.iv_video_control_fast)
        layoutLongPressFast = rootView.findViewById(R.id.layout_video_control_fast)
        // 长按快进是gif图，需要用glide加载
        val imageLoader = ImageLoader.Builder(context)
            .components {
                if (Build.VERSION.SDK_INT >= 28) {
                    add(ImageDecoderDecoder.Factory())
                } else {
                    add(GifDecoder.Factory())
                }
            }
            .build()
        Coil.setImageLoader(imageLoader)
        ivLongPress.load(R.drawable.ic_wb_mix_fast_skip)
        //创建手势控制
        mGestureControl = GestureControl(context, this)
        mGestureControl?.setMultiWindow(isInMultiWindow)
        mGestureControl?.setView(this)
        //设置监听
        mGestureControl?.setOnGestureControlListener(object : GestureListener {
            override fun onHorizontalDistance(downX: Float, nowX: Float) {
                //其他手势如果锁住了就不回调了。
                if (!isFirstFrameStart) return
                if (isScreenLocked) return
                mOutGestureListener?.onHorizontalDistance(downX, nowX)
            }

            override fun onLeftVerticalDistance(downY: Float, nowY: Float) {
                if (!isFirstFrameStart) return
                if (isScreenLocked) return
                mOutGestureListener?.onLeftVerticalDistance(downY, nowY)
            }

            override fun onRightVerticalDistance(downY: Float, nowY: Float) {
                if (!isFirstFrameStart) return
                if (isScreenLocked) return
                mOutGestureListener?.onRightVerticalDistance(downY, nowY)
            }

            override fun onGestureEnd() {
                if (!isFirstFrameStart) return
//                if (mIsFullScreenLocked) return;
                mOutGestureListener?.onGestureEnd()
            }

            override fun onSingleTap() {
                if (!isFirstFrameStart) return
                mOutGestureListener?.onSingleTap()
            }

            override fun onDoubleTap() {
                if (!isFirstFrameStart) return
                if (isScreenLocked) return
                mOutGestureListener?.onDoubleTap()
            }

            override fun onLongPress() {
                if (!isFirstFrameStart) return
                if (isScreenLocked) return
                mOutGestureListener?.onLongPress()
            }
        })
    }

    /**
     * 设置是否锁定全屏了。锁定全屏的话，除了单击手势有响应，其他都不会有响应。
     *
     * @param locked true：锁定。
     */
    fun setScreenLockStatus(locked: Boolean) {
        isScreenLocked = locked
    }
    //    public void setHideType(HideType hideType) {
    //        this.mHideType = hideType;
    //    }
    /**
     * 设置是否处于分屏模式
     * @param isInMultiWindow  true,分屏模式,false不是分屏模式
     */
    fun setMultiWindow(isInMultiWindow: Boolean) {
        this.isInMultiWindow = isInMultiWindow
        mGestureControl?.setMultiWindow(this.isInMultiWindow)
    }

    /**
     * 显示长按快进UI
     */
    fun showLongPressFast(){
        layoutLongPressFast.visibility = View.VISIBLE
    }
    /**
     * 隐藏长按快进UI
     */
    fun hideLongPressFast(){
        layoutLongPressFast.visibility = View.INVISIBLE
    }


    override fun onVodInit(vararg init: Any?): Boolean { return false}
    override fun onVodPrepared(vararg prepared: Any) {}
    override fun onVodStart() {}
    override fun onVodFirstFrameStart() {
        isFirstFrameStart = true
    }
    override fun onVodPause() {}
    override fun onVodStop() {
        isFirstFrameStart = false
    }

    override fun onVodError(vararg params: Any) {
        isFirstFrameStart = false
    }

    override fun onVodComplete() {

    }

    override fun onVodRelease() {
        isFirstFrameStart = false
    }
    override fun onVodLockedScreen(vararg params: Any) {
        if (params.isEmpty()) return
        isScreenLocked = params[0] as Boolean
    }
    override fun onVodFullScreen(vararg params: Any) {}
    override fun onVodLoadingBegin() {
        
    }

    override fun onVodLoadingProgress(vararg params: Any) {
        
    }

    override fun onVodLoadingEnd() {
        
    }

    override fun onVodBackPress(): Boolean {
        return false
    }

    override fun onEpisodeSelected() {

    }

    override fun onVodShowUnlockUI(isDisplayUI: Boolean) {

    }

    override fun onVodSkits() {

    }

    override fun onVodSwitchDisplayControlView() {}
    
    
    interface GestureListener {
        /**
         * 水平滑动距离
         * @param downX 按下位置
         * @param nowX  当前位置
         */
        fun onHorizontalDistance(downX: Float, nowX: Float)

        /**
         * 左边垂直滑动距离
         *
         * @param downY 按下位置
         * @param nowY  当前位置
         */
        fun onLeftVerticalDistance(downY: Float, nowY: Float)

        /**
         * 右边垂直滑动距离
         * @param downY 按下位置
         * @param nowY  当前位置
         */
        fun onRightVerticalDistance(downY: Float, nowY: Float)

        /**
         * 手势结束
         */
        fun onGestureEnd()

        /**
         * 单击事件
         */
        fun onSingleTap()

        /**
         * 双击事件
         */
        fun onDoubleTap()

        /**
         * 长安
         */
        fun onLongPress()
    }

    /**
     * 设置手势监听事件
     * @param gestureListener 手势监听事件
     */
    fun setOnGestureListener(gestureListener: GestureListener?) {
        mOutGestureListener = gestureListener
    } //    @Override
    //    public void reset() {
    //        mHideType = null;
    //    }
    //
    //    @Override
    //    public void show() {
    //        if (mHideType == HideType.End) {
    //            //如果是由于错误引起的隐藏，那就不能再展现了
    //        } else {
    //            setVisibility(VISIBLE);
    //        }
    //    }
    //
    //    @Override
    //    public void hide(HideType hideType) {
    //        if (mHideType != HideType.End) {
    //            mHideType = hideType;
    //        }
    //        setVisibility(GONE);
    //    }
    //
    //    @Override
    //    public void setScreenModeStatus(AliyunScreenMode mode) {
    //
    //    }
}